package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.xr.common.constant.OrderStatusConstants;
import com.xr.miniprogram.domain.MiniProgramOrder;
import com.xr.miniprogram.domain.MiniProgramRefund;
import com.xr.miniprogram.mapper.MiniProgramRefundMapper;
import com.xr.miniprogram.service.IMiniProgramOrderService;
import com.xr.miniprogram.service.IMiniProgramRefundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author da
 * @since 2023-10-25
 */
@Service
public class MiniProgramRefundServiceImpl extends ServiceImpl<MiniProgramRefundMapper, MiniProgramRefund> implements IMiniProgramRefundService {


    @Resource
    IMiniProgramOrderService orderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRefund(String result) {
        Gson gson = new Gson();
        HashMap resultMap = gson.fromJson(result, HashMap.class);
        MiniProgramRefund miniProgramRefund = this.getOne(new QueryWrapper<MiniProgramRefund>().eq("out_refund_no", resultMap.get("out_refund_no")));

        miniProgramRefund.setRefundId((String) resultMap.get("refund_id"));
        //查询退款和申请退款中的返回函数
        if (resultMap.get("status") != null){
            miniProgramRefund.setRefundStatus((String) resultMap.get("status"));
            miniProgramRefund.setContent(result);
        }
        //退款回调中的回调参数
        if (resultMap.get("refund_status") != null){
            miniProgramRefund.setRefundStatus((String) resultMap.get("refund_status"));
            miniProgramRefund.setNotify(result);
        }

        baseMapper.updateById(miniProgramRefund);
    }

    @Override
    public MiniProgramRefund createRefundByOrderNo(String orderNo, String reason) {
        MiniProgramOrder miniProgramOrder = orderService.getOne(new QueryWrapper<MiniProgramOrder>().eq("order_number",orderNo));
        MiniProgramRefund miniProgramRefund = new MiniProgramRefund();
        //生成退款编号
        long timestamp = new Date().getTime(); // 获取当前时间戳
        int randomNum = new Random().nextInt(1000); // 获取0-999之间的随机数
        String orderNumber = "TK_" + timestamp + randomNum; // 拼接订单号
        miniProgramRefund.setOrderNumber(miniProgramOrder.getOrderNumber());
        miniProgramRefund.setTotalMoney(miniProgramOrder.getOrderPrice());
        miniProgramRefund.setOutRefundNo(orderNumber);
        miniProgramRefund.setReason(reason);
        miniProgramRefund.setRefundMoney(miniProgramOrder.getOrderPrice());
        baseMapper.insert(miniProgramRefund);
        return miniProgramRefund;
    }

    @Override
    public MiniProgramRefund getRefundByOrderNumber(String orderNumber) {
        return getOne(new LambdaQueryWrapper<MiniProgramRefund>().eq(MiniProgramRefund::getOrderNumber, orderNumber));
    }
}
